﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Configuration;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;

namespace PromotionEngine
{
    public partial class Doc_Line_Property : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.DocLineProperty> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.DocLineProperty> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        public class ResponseDetail
        {
            public List<Model.DocLinePropertyDetail> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.DocLinePropertyDetail> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocLinePropertys()
        {
            List<Model.DocLineProperty> lstDocLineProperty = new List<Model.DocLineProperty>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocLineProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString(),
            ConfigurationManager.AppSettings["PropertyValueType"].ToString(), bool.Parse(HttpContext.Current.Session["IsAdmin"].ToString()));
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLineProperty docLineProperty = new Model.DocLineProperty();
                    docLineProperty.CompanyCode = r["CompanyCode"].ToString();
                    docLineProperty.CompanyName = r["CompanyName"].ToString();
                    docLineProperty.PropertyCode = r["PropertyCode"].ToString();
                    docLineProperty.PropertyName = r["PropertyName"].ToString();
                    docLineProperty.PropertyID = r["PropertyID"].ToString();
                    docLineProperty.CreatedUserID = r["CreatedUserID"].ToString();
                    docLineProperty.PropertyValueType = r["PropertyValueType"].ToString();
                    docLineProperty.ReferenceName = r["ReferenceName"].ToString();
                    lstDocLineProperty.Add(docLineProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLineProperty };
                return new Response(lstDocLineProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocLineProperty(string oDocLineProperty, bool isInsert)
        {
            Model.DocLineProperty docLineProperty = JsonConvert.DeserializeObject<Model.DocLineProperty>(oDocLineProperty);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        docLineProperty.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        docLineProperty.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocLineProperty(docLineProperty.PropertyID, docLineProperty.PropertyCode, docLineProperty.PropertyName
                , docLineProperty.CompanyCode, docLineProperty.CreatedUserID,docLineProperty.PropertyValueType,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocLineProperty(string propertyID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonDocLineProperty(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetDocLinePropertyDetails(string propertyID)
        {
            List<Model.DocLinePropertyDetail> lstDocLinePropertyDetail = new List<Model.DocLinePropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocPropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLinePropertyDetail docLinePropertyDetail = new Model.DocLinePropertyDetail();
                    docLinePropertyDetail.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docLinePropertyDetail.PropertyDetailName = r["PropertyDetailName"].ToString();
                    docLinePropertyDetail.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docLinePropertyDetail.PropertyID = r["PropertyID"].ToString();
                    docLinePropertyDetail.CreatedUserID = r["CreatedUserID"].ToString();

                    lstDocLinePropertyDetail.Add(docLinePropertyDetail);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLinePropertyDetail };
                return new ResponseDetail(lstDocLinePropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetAllDocLinePropertyDetails()
        {
            List<Model.DocLinePropertyDetail> lstDocLinePropertyDetail = new List<Model.DocLinePropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocPropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocLinePropertyDetail docLinePropertyDetail = new Model.DocLinePropertyDetail();
                    docLinePropertyDetail.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docLinePropertyDetail.PropertyDetailName = r["PropertyDetailName"].ToString();
                    docLinePropertyDetail.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docLinePropertyDetail.PropertyID = r["PropertyID"].ToString();
                    docLinePropertyDetail.CreatedUserID = r["CreatedUserID"].ToString();

                    lstDocLinePropertyDetail.Add(docLinePropertyDetail);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLinePropertyDetail };
                return new ResponseDetail(lstDocLinePropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocLinePropertyDetail(string oDocLinePropertyDetail, bool isInsert)
        {
            Model.DocPropertyDetail docLinePropertyDetail = JsonConvert.DeserializeObject<Model.DocPropertyDetail>(oDocLinePropertyDetail);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        docLinePropertyDetail.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocLinePropertyDetail(docLinePropertyDetail.PropertyID, docLinePropertyDetail.PropertyDetailCode, docLinePropertyDetail.PropertyDetailName
                                                    , docLinePropertyDetail.PropertyDetailID, docLinePropertyDetail.CreatedUserID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocLinePropertyDetail(string propertyDetailID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonDocLinePropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyDetailID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}